#!/bin/bash

if [ $# -lt 1 ]; then
	echo "Usage: $0 dir"
	exit 0
fi

dirname=$1

for file in $dirname/*.txt
do
	encoding=`file "$file" --mime-encoding -b`
	[ "${encoding}" = "utf-8" ] && continue

	iconv -f gb18030 -t utf8 -o "$file.utf8" "$file"

	if [ $? -eq 0 ]; then
		mv "$file.utf8" "$file"
	else
		echo "$file: ${encoding}"
	fi
done


